'use strict';
//服务记录
import React,{Component} from 'react';
import Header from '../../common/header';
import * as URLconstant from '../../constant/URLconstant'
import Util from '../../common/util';
import {
    View,
    Text,
    TouchableOpacity,
    Image,
    StyleSheet,
    ScrollView, ListView
} from 'react-native';
export default class serviceRecord extends Component{
    // 构造
      constructor(props) {
        super(props);
        // 初始状态
        this.state = {
            dataSource2: new ListView.DataSource({rowHasChanged: (row1, row2) => row1 !== row2}),
            dataSource: [],
            page:1,
            limit:10,
            isOver:false,
            loaded:true,
        };
        this.onEndReached=this.onEndReached.bind(this)
          this._renderRow=this._renderRow.bind(this)
          this.renderFooter=this.renderFooter.bind(this)
      }

    componentDidMount() {
        this.getRecord()
    }


      getRecord(){
          let url = URLconstant.GET_SERVICE_RECORD
          let datakey=[]
          datakey['user_id']=this.props.user_id
            let role_id = this.props.role_id
          LOG('role_id==='+role_id)
          let status = 0
          let identity = 'user'
          if(role_id==1){
              status=1
              identity='dz'
          }else if(role_id==3){
              status=1
              identity='dy'
          }
          let page = this.state.page
          let limit = this.state.limit
          datakey['page']=page
          datakey['limit']=limit
          datakey['identity']=identity
          datakey['status']=status
          Util.get(url,datakey,this.getRecordSuc.bind(this),this.errorCallback.bind(this))
      }

    getRecordSuc(datas){
        if(datas.status==0){
            let isOver = false
            let newDatas
            let total={}
            if(datas.data.length==0){
                newDatas=[]
                total.limit=10
            }else{
                newDatas = datas.data.list;
                total = datas.data.total
            }

            let oldDatas = this.state.dataSource;
            if(newDatas.length<total.limit){
                isOver=true
            }
            Array.prototype.push.apply(oldDatas, newDatas);
            this.setState({
                isOver:isOver,
                dataSource2: this.state.dataSource2.cloneWithRows(oldDatas),
                dataSource: oldDatas,
                loaded:false,
                page:this.state.page+1
            })
        }
    }
    errorCallback(err){

    }

    onEndReached(){
        let isOver = this.state.isOver
        if(!isOver){
            this.getRecord()
        }
    }

    renderFooter(){
        return (
            <View style={{height:30,alignItems:'center',justifyContent:'center'}}>
                <Text style={{fontSize:12,color:"#969696"}}>{this.state.isOver?"没有更多数据了":"正在加载中"}</Text>
            </View>
        )
    }

    _renderRow(item){
          return(
              <View key={item.id} style={styles.record_item}>
                  <View style={styles.item_header}>
                      <Text style={{fontSize:14,color:"#000"}}>服务记录</Text>
                      {
                          item.type==0?
                              <Text style={{fontSize:12,color:"#f65061"}}>未使用</Text>:
                              <Text style={{fontSize:12,color:"#f65061"}}>{item.end_fuwu_time==0?"服务中":'已服务'}</Text>
                      }
                  </View>
                  <View style={styles.item_view}>
                    <View style={styles.list_item}>
                        <Text style={styles.item_txt}>服务项目:{item.quan_name}</Text>
                    </View>
                      <View style={styles.list_item}>
                          <Text style={styles.item_txt}>服务券码:{item.vouche_sn}</Text>
                      </View>
                      <View style={styles.list_item}>
                          <Text style={styles.item_txt}>服务人员:{item.waiter}</Text>
                      </View>
                      <View style={styles.list_item}>
                          <Text style={styles.item_txt}>订单编号:{item.order_sn}</Text>
                      </View>
                      <View style={[styles.list_item,styles.no_border]}>
                          <Text style={styles.item_txt}>服务时间:{item.start_fuwu_time}</Text>
                      </View>
                  </View>
              </View>
          )
    }

      render(){
          return(
              <View style={{flex:1,backgroundColor:"#f0f0f0"}}>
                  <Header navigator={this.props.navigator} initObj={{ title: '服务记录',id:2}}/>
                  {
                      this.state.loaded?
                          <View style={{flex:1}}>
                              {Util.loading()}
                          </View>:
                          <View style={{flex:1}}>
                              {
                                  this.state.dataSource.length==0?
                                      <View style={{flex:1,alignItems:'center',justifyContent:'center'}}>
                                          <Text>没有服务记录</Text>
                                      </View>
                                      :
                                      <ListView
                                          dataSource={this.state.dataSource2}
                                          onEndReached={this.onEndReached}
                                          renderRow={this._renderRow}
                                          renderFooter={this.renderFooter}
                                          style={{paddingHorizontal:10}}
                                      />
                              }
                          </View>
                  }
              </View>
          )
      }
}
var styles =StyleSheet.create({
    record_item:{
        backgroundColor:"#fff",
        borderWidth:1,
        borderColor:"#d7d7d7",
        marginTop:10
    },
    item_header:{
        paddingHorizontal:10,
        borderBottomWidth:1,
        borderBottomColor:"#e0e0e0",
        flexDirection:'row',
        height:40,
        alignItems:'center',
        justifyContent:'space-between'
    },
    item_view:{
        paddingHorizontal:10,
    },
    list_item:{
        height:30,
        borderBottomWidth:1,
        borderBottomColor:"#d7d7d7",
        alignItems:'center',
        flexDirection:"row"
    },
    no_border:{
        borderBottomWidth:0
    },
    item_txt:{
        fontSize:12,
        color:"#666"
    }
})